import Vue from 'vue'
import Router from 'vue-router'
import Login from './views/login.vue'
import Main from './views/main.vue'
import store from './store.js'
Vue.use(Router)

const routesTable=[
	{
		path: '/login',
		name: 'login',
		component:Login
	},{
		path: '/main',
		name: 'main',
		component: Main,
		meta: { requiresAuth: true },
		children:[{
				path: '/messageList',
				name: 'messageList',
				component: () => import('./views/messageList.vue')
			},{
				path: '/contacts',
				name: 'contacts',
				component: () => import('./views/contacts.vue')
			},{
				path: '/my',
				name: 'my',
				component: () => import('./views/my.vue')
			}
		]
	},{
		path: '/chat',
		name: 'chat',
		meta: { requiresAuth: true },
		component: () => import('./views/chat.vue'),
	}
];

const router = new Router({
  		mode: 'history',
  		base: process.env.BASE_URL,
  		routes:routesTable
});

router.beforeEach((to, from, next) => {
		if(to.matched.some(record => record.meta.requiresAuth)) {
 				if(!store.state.isLogin){
 						next({path: '/login'});
 				}else{
      			next();
	    	};
 		}else{
 				next();
 		}
});

export default router;
